Skip to content

MkAdmonition

:fa-brands-github: Show source on GitHub

Admonition info box.

Example: Types

Jinja

{{ "Different types." | MkAdmonition(typ="warning") }}

Python

MkAdmonition(None, typ='warning')

Warning

Different types.

!!! warning
    Different types.
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Different types.</p>
</div>
.. warning:: 
    Different types.
> [!WARNING]
> Different types.
MkAdmonition
╰── MkText('Different types.')

Example: Collapsible and expandable

Jinja

{{ "Admonitions can also be collapsible." | MkAdmonition(collapsible=True, title="Expand me!", expanded=True) }}

Python

MkAdmonition(None, title='Expand me!', collapsible=True, expanded=True)
Expand me!

Admonitions can also be collapsible.

???+ info "Expand me!"
    Admonitions can also be collapsible.
<details class="info" open>
<summary>Expand me!</summary>
<p>Admonitions can also be collapsible.</p>
</details>
.. info:: Expand me!
    Admonitions can also be collapsible.
<details open>
<summary>Expand me!</summary>

Admonitions can also be collapsible.

</details>
MkAdmonition
╰── MkText('Admonitions can also be collapsible.')

Example: Inlined

Jinja

{{ "Inlined" | MkAdmonition(inline="left") }}

Python

MkAdmonition(None, inline='left')

Info

Inlined

!!! info inline
    Inlined
<div class="admonition info inline">
<p class="admonition-title">Info</p>
<p>Inlined</p>
</div>
.. info:: 
    Inlined
> [!NOTE]
> Inlined
MkAdmonition
╰── MkText('Inlined')

Bases: MkContainer

__init__

__init__(
    content: str | list[MkNode] | MkNode,
    *,
    typ: AdmonitionTypeStr | str = "info",
    title: str | None = None,
    collapsible: bool = False,
    expanded: bool = False,
    inline: Literal["left", "right"] | None = None,
    **kwargs: Any
) -> None

Parameters:

Name Type Description Default
content str | list[MkNode] | MkNode

Admonition content

required
typ AdmonitionTypeStr | str

Admonition type

'info'
title str | None

Optional Admonition title

None
collapsible bool

Whether Admontion can get collapsed by user

False
expanded bool

Initial state if collapsible is set

False
inline Literal['left', 'right'] | None

Whether admonition should rendered as inline block

None
kwargs Any

Keyword arguments passed to parent

{}

get_content

get_content() -> NodeContent
Name Children Inherits
MkContainer
mknodes.basenodes.mkcontainer
A node containing other MkNodes.
graph TD
  178616935["mkadmonition.MkAdmonition"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  2751680898["builtins.object"]
  835538629 --> 178616935
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkadmonition/metadata.toml
[metadata]
icon = "octicon:info-16"
name = "MkAdmonition"

[requirements.extension.admonition]

[requirements.extension."pymdownx.details"]

[requirements.extension."pymdownx.superfences"]

[examples.annotations]
title = "Annotations"
python = """
import mknodes as mk
node = mk.MkAdmonition("MkAdmonitions can carry annotations(1).")
node.annotations[1] = "Super handy!"
node
"""

[examples.types]
title = "Types"
jinja = """
{{ "Different types." | MkAdmonition(typ="warning") }}
"""

[examples.collapsible]
title = "Collapsible and expandable"
jinja = """
{{ "Admonitions can also be collapsible." | MkAdmonition(collapsible=True, title="Expand me!", expanded=True) }}
"""

[examples.inlined]
title = "Inlined"
jinja = """
{{ "Inlined" | MkAdmonition(inline="left") }}
"""

[output.markdown]
template = """
{% if node.title or node.get_items() %}
{{ node.title_line }}
{{ node.get_items() | join(node.block_separator) | indent(first=True) }}
{% endif %}
"""

[output.block]
template = """
{{ node.fence_boundary }} admonition {{ node.typ }}{{ node.title | add(" | ") }}
{{ node.get_items() | join }}
{{ node.fence_boundary }}
"""

[output.rst]
template = """
.. {{ node.typ }}:: {{ node.title or "" }}
{{ node.get_items() | join(node.block_separator) | indent(first=True) }}
"""
mknodes.basenodes.mkadmonition.MkAdmonition
class MkAdmonition(mkcontainer.MkContainer):
    """Admonition info box."""

    ICON = "octicons/info-16"
    ATTR_LIST_SEPARATOR = "    "
    REQUIRED_EXTENSIONS = [
        resources.Extension("admonition"),
        resources.Extension("pymdownx.details"),
        resources.Extension("pymdownx.superfences"),
    ]

    def __init__(
        self,
        content: str | list[mknode.MkNode] | mknode.MkNode,
        *,
        typ: datatypes.AdmonitionTypeStr | str = "info",
        title: str | None = None,
        collapsible: bool = False,
        expanded: bool = False,
        inline: Literal["left", "right"] | None = None,
        **kwargs: Any,
    ) -> None:
        """Constructor.

        Args:
            content: Admonition content
            typ: Admonition type
            title: Optional Admonition title
            collapsible: Whether Admontion can get collapsed by user
            expanded: Initial state if collapsible is set
            inline: Whether admonition should rendered as inline block
            kwargs: Keyword arguments passed to parent
        """
        self.typ = typ
        self.title = title
        self.collapsible = collapsible
        self.inline = inline
        self.expanded = expanded
        super().__init__(content=content, **kwargs)

    def attach_annotations(self, text: str) -> str:
        # we deal with attaching annotations ourselves.
        return text

    @property
    def title_line(self) -> str:
        block_start = "???" if self.collapsible else "!!!"
        if self.collapsible and self.expanded:
            block_start += "+"
        if self.inline:
            inline_label = " inline" if self.inline == "left" else " inline end"
        else:
            inline_label = ""
        ann_marker = " annotate" if self.annotations else ""
        title = f' "{self.title}"' if self.title is not None else ""
        optional = ann_marker + inline_label
        return f"{block_start} {self.typ}{optional}{title}"

    def get_content(self) -> resources.NodeContent:
        """Single-pass: get markdown with custom admonition formatting and resources."""
        items = self.get_items()
        if not items and not self.title:
            return resources.NodeContent(
                markdown="",
                resources=self._build_node_resources(),
            )

        # Collect content from children
        renderer = rendering.current()
        child_contents = [renderer.render_child(item) for item in items]
        child_markdowns = [content.markdown for content in child_contents]

        text = "\n".join(child_markdowns)
        indented = textwrap.indent(text, "    ")
        annotations = f"\n{self.annotations}\n" if self.annotations else ""
        md = f"{self.title_line}\n{indented}\n{annotations}"

        # Aggregate resources
        aggregated = self._build_node_resources()
        for child_content in child_contents:
            aggregated.merge(child_content.resources)

        return resources.NodeContent(markdown=md, resources=aggregated)

    def to_md_unprocessed(self) -> str:
        content = self.get_content()
        return content.markdown